<?php
// 引入头部
require_once 'includes/header.php';

// 获取今日统计数据
$today = date('Y-m-d');
$today_start = $today . ' 00:00:00';
$today_end = $today . ' 23:59:59';

// 今日待处理订单数
$pending_orders = Database::fetch("SELECT COUNT(*) as count FROM orders WHERE status = 'paid' AND created_at BETWEEN ? AND ?", [$today_start, $today_end]);
$pending_orders = $pending_orders ? $pending_orders['count'] : 0;

// 今日制作中订单数
$cooking_orders = Database::fetch("SELECT COUNT(*) as count FROM orders WHERE status = 'cooking' AND created_at BETWEEN ? AND ?", [$today_start, $today_end]);
$cooking_orders = $cooking_orders ? $cooking_orders['count'] : 0;

// 今日已完成订单数
$completed_orders = Database::fetch("SELECT COUNT(*) as count FROM orders WHERE status = 'completed' AND created_at BETWEEN ? AND ?", [$today_start, $today_end]);
$completed_orders = $completed_orders ? $completed_orders['count'] : 0;

// 获取最近5个待处理订单
$recent_orders = Database::fetchAll("SELECT o.*, u.name as member_name 
                                     FROM orders o 
                                     LEFT JOIN users u ON o.user_id = u.id 
                                     WHERE o.status IN ('paid', 'cooking') 
                                     ORDER BY o.created_at DESC LIMIT 5");

// 获取每个订单的详细商品
foreach ($recent_orders as $key => $order) {
    $items = Database::fetchAll("SELECT * FROM order_items WHERE order_id = ?", [$order['id']]);
    $recent_orders[$key]['items'] = $items;
}
?>

<div class="container-fluid">
    <div class="row mb-4">
        <div class="col-md-4">
            <div class="stat-card stat-pending">
                <div class="stat-title">待处理订单</div>
                <div class="stat-value"><?php echo $pending_orders; ?></div>
                <a href="orders.php" class="btn btn-primary btn-sm">立即处理</a>
            </div>
        </div>
        
        <div class="col-md-4">
            <div class="stat-card stat-cooking">
                <div class="stat-title">制作中订单</div>
                <div class="stat-value"><?php echo $cooking_orders; ?></div>
                <a href="orders.php" class="btn btn-warning btn-sm">查看订单</a>
            </div>
        </div>
        
        <div class="col-md-4">
            <div class="stat-card stat-completed">
                <div class="stat-title">今日已完成</div>
                <div class="stat-value"><?php echo $completed_orders; ?></div>
                <a href="history.php" class="btn btn-success btn-sm">历史订单</a>
            </div>
        </div>
    </div>
    
    <div class="row">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0">最近待处理订单</h5>
                </div>
                <div class="card-body">
                    <?php if (empty($recent_orders)): ?>
                    <div class="alert alert-info">
                        <i class="fas fa-info-circle mr-2"></i> 暂无待处理订单
                    </div>
                    <?php else: ?>
                    <div class="orders-grid">
                        <?php foreach ($recent_orders as $order): ?>
                        <div class="order-card" id="order-<?php echo $order['id']; ?>" data-order-id="<?php echo $order['id']; ?>">
                            <div class="order-header">
                                <div class="order-number">订单 #<?php echo $order['queue_number']; ?></div>
                                <div class="order-status status-<?php echo $order['status']; ?>">
                                    <?php
                                    switch ($order['status']) {
                                        case 'paid': echo '待处理'; break;
                                        case 'cooking': echo '制作中'; break;
                                        default: echo $order['status']; break;
                                    }
                                    ?>
                                </div>
                            </div>
                            <div class="order-items">
                                <?php foreach ($order['items'] as $item): ?>
                                <div class="order-item" data-item-id="<?php echo $item['id']; ?>">
                                    <div class="order-item-name"><?php echo htmlspecialchars($item['product_name']); ?></div>
                                    <div class="order-item-quantity">x<?php echo $item['quantity']; ?></div>
                                    <div class="order-item-status status-<?php echo $item['status']; ?>">
                                        <?php
                                        switch ($item['status']) {
                                            case 'pending': echo '待处理'; break;
                                            case 'cooking': echo '制作中'; break;
                                            case 'completed': echo '已完成'; break;
                                            default: echo $item['status']; break;
                                        }
                                        ?>
                                    </div>
                                    <div class="order-item-actions">
                                        <?php if ($item['status'] !== 'completed'): ?>
                                        <button class="btn btn-sm btn-success btn-complete-item" data-order-item-id="<?php echo $item['id']; ?>">
                                            完成
                                        </button>
                                        <?php else: ?>
                                        <span class="text-success"><i class="fas fa-check-circle"></i> 已完成</span>
                                        <?php endif; ?>
                                    </div>
                                </div>
                                <?php endforeach; ?>
                            </div>
                            <div class="order-footer">
                                <div class="order-time"><?php echo date('m-d H:i', strtotime($order['created_at'])); ?></div>
                                <div class="order-actions">
                                    <?php if ($order['status'] === 'paid'): ?>
                                    <button class="btn btn-primary btn-start-cooking" data-order-id="<?php echo $order['id']; ?>">
                                        开始制作
                                    </button>
                                    <?php elseif ($order['status'] === 'cooking'): ?>
                                    <button class="btn btn-success btn-complete-all-items" data-order-id="<?php echo $order['id']; ?>">
                                        <i class="fas fa-utensils"></i> 所有菜品完成
                                    </button>
                                    <button class="btn btn-primary btn-complete-order" data-order-id="<?php echo $order['id']; ?>" <?php echo allItemsCompleted($order['items']) ? '' : 'disabled'; ?>>
                                        完成订单
                                    </button>
                                    <?php else: ?>
                                    <button class="btn btn-outline-success" disabled>已完成</button>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                        <?php endforeach; ?>
                    </div>
                    
                    <div class="text-center mt-3">
                        <a href="orders.php" class="btn btn-primary">
                            <i class="fas fa-list mr-1"></i> 查看全部待处理订单
                        </a>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>

<?php
// 检查订单项是否都已完成
function allItemsCompleted($items) {
    foreach ($items as $item) {
        if ($item['status'] !== 'completed') {
            return false;
        }
    }
    return true;
}

// 引入页脚
require_once 'includes/footer.php';
?> 